package com.ly.user_system.service.impl;

import com.ly.user_system.base.BaseServiceImpl;
import com.ly.user_system.detail.BaseSysRoleDetail;
import com.ly.user_system.dto.BaseSysRoleDTO;
import com.ly.user_system.dto.BaseSysRoleDepartmentDTO;
import com.ly.user_system.dto.BaseSysRoleMenuDTO;
import com.ly.user_system.entity.BaseSysRole;
import com.ly.user_system.mapper.BaseSysRoleMapper;
import com.ly.user_system.req.BaseSysRoleQuery;
import com.ly.user_system.service.BaseSysRoleDepartmentService;
import com.ly.user_system.service.BaseSysRoleMenuService;
import com.ly.user_system.service.BaseSysRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class BaseSysRoleServiceImpl extends BaseServiceImpl<BaseSysRole, BaseSysRoleDetail, BaseSysRoleDTO, BaseSysRoleQuery, BaseSysRoleMapper> implements BaseSysRoleService {


    @Override
    protected void init(Class<BaseSysRole> entityClass, Class<BaseSysRoleDTO> dtoClass, Class<BaseSysRoleDetail> detailClass, Function<BaseSysRole, BaseSysRoleDetail> detailFunction) {
        super.init(BaseSysRole.class,BaseSysRoleDTO.class,BaseSysRoleDetail.class,detailFunction);
    }

    @Resource
    private BaseSysRoleDepartmentService roleDepartmentService;

    @Resource
    private BaseSysRoleMenuService roleMenuService;




    @Override
    public Boolean saveBatch(List<BaseSysRoleDTO> list) {
        return super.saveBatch(list,(entityList)->{
            // 先保存role关键数据再保存冗余关联数据（角色与菜单，角色与部门）
            list.forEach(item->{
                roleDepartmentService.saveBatch(item.getDepartmentIdList().stream().map(departmentId-> new BaseSysRoleDepartmentDTO(departmentId, item.getId())).collect(Collectors.toList()));
                roleMenuService.saveBatch(item.getMenuIdList().stream().map(menuId-> new BaseSysRoleMenuDTO(menuId, item.getId())).collect(Collectors.toList()));
            });
        });
    }


    @Override
    public Boolean deleteBatch(List<Long> ids) {
        return super.deleteBatch(ids,(idList)->{

        });
    }


}